﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CollegeReview.Data;
using CollegeReview.DataModels;
using CollegeReview.Web.IDataAdapters.Interfaces;

namespace CollegeReview.Web.IDataAdapters.Adapters
{
    public class CollegeReviewAdapter : ICollegeReviewAdapter
    {
        public List<College> GetAllColleges()
        {
            List<College> colleges;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                colleges = db.Colleges.OrderByDescending(x => x.AvgRating).ToList();
            }
            return colleges;
        }

        public College AddCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Add(college);
                db.SaveChanges();
            }
            return college;
        }

        public College GetCollege(int id)
        {
            College college;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Find(id);
            }
            return college;
        }

        public College UpdateCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College oldCollege = db.Colleges.Find(college.Id);
                oldCollege.setCollegeProps(college);
                db.SaveChanges();
            }
            return college;
        }

        public void DeleteCollege(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.Find(id);
                db.Colleges.Remove(college);
                db.SaveChanges();
            }
        }

        public List<Review> GetAllReviews()
        {
            List<Review> reviews;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                reviews = db.Reviews.OrderBy(x => x.Timestamp).ToList();
            }
            return reviews;    
        }

        public Review AddReview(Review review)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Add(review);
                db.SaveChanges();
            }
            return review;
        }

        public Review GetReview(int id)
        {
            Review review;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Find(id);
            }
            return review;
        }

        public void DeleteReview(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Review review = db.Reviews.Find(id);
                db.Reviews.Remove(review);
                db.SaveChanges();
            }
        }
    }
}